package com.wbq.datastructuredemo.adapter;

import com.wbq.datastructuredemo.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainUIAdapter extends BaseAdapter{
	private static final String TAG = "MainUIAdapter";
	private Context context;
	private LayoutInflater inflater;
	private static ImageView iv_icon;
	private static TextView tv_name;
	private SharedPreferences sp;
	public MainUIAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
	}

	private static String[] names ={"数构基础","在线问答","动画演示",
		"视频学习","编写运行","综合排行","自定义","高级工具","设置中心"};
	private static int[] icons = {R.drawable.app,R.drawable.atools,R.drawable.callmsgsafe,R.drawable.netmanager,
		R.drawable.safe,R.drawable.sysoptimize,R.drawable.taskmanager,R.drawable.trojan,R.drawable.settings};
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return names.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//getView调用了多少次
		Log.i(TAG, "getView:"+position);
		View view = inflater.inflate(R.layout.mainscreen_item, null);
		//使用静态的变量引用减少内存中申请的引用个数
		iv_icon = (ImageView) view.findViewById(R.id.iv_main_icon);
		tv_name = (TextView) view.findViewById(R.id.tv_mian_function);
		iv_icon.setImageResource(icons[position]);
		tv_name.setText(names[position]);
		String name = sp.getString("lost_name_"+position, null);
		if(name!=null){
			tv_name.setText(name);
		}
		return view;
	}
	
}
